package com.oocl.o2o.client.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import oracle.sql.OffsetDST;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.o2o.client.entity.Customer;
import com.oocl.o2o.client.entity.Food;
import com.oocl.o2o.client.entity.Merchant;
import com.oocl.o2o.client.entity.OrderInfo;
import com.oocl.o2o.client.entity.OrderItem;
import com.oocl.o2o.client.entity.OrderStatus;
import com.oocl.o2o.client.entity.ReceiverInfo;
import com.oocl.o2o.client.service.FoodService;
import com.oocl.o2o.client.service.MerchantService;
import com.oocl.o2o.client.service.OrderInfoService;
import com.oocl.o2o.client.service.ReceiverInfoService;
import com.oocl.o2o.client.vo.OrderItemVO;
import com.oocl.o2o.client.vo.ReceiverInfoVO;

@Controller
public class OrderInfoCotroller {

	@Resource
	private OrderInfoService orderInfoService;
	@Resource
	private MerchantService merchantService;
	@Resource
	private FoodService foodService;
	@Resource
	private ReceiverInfoService receiverInfoService;
	
	@ResponseBody
	@GetMapping("/orderInfo/id/{id}")
	public OrderInfo findById(@RequestBody @PathVariable("id") String id){
		OrderInfo orderInfo = new OrderInfo();
		orderInfo = orderInfoService.findById(id);
		System.out.println("查找到的信息为： orderInfo "+orderInfo.getId()+" "+orderInfo.getCustomer().getUsername());
		System.out.println(orderInfo.getReceiverInfo().toString());
		return orderInfo;
		
	}
	
	//用户点击了结算按钮，来到这里，把订单信息写到数据库，因此需要在service完成 VO 到后台数据结构的转换
	
	@PostMapping("/orderInfo")
	public String add( @RequestBody ReceiverInfoVO receiverInfoVO, HttpSession session){
		ReceiverInfo receiverInfo = receiverInfoService.findById(receiverInfoVO.getId());
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");	
		Customer customer = (Customer) session.getAttribute("customer");
		//获取商家的名字：
		Merchant merchant = new Merchant();
		Date orderDate = new Date();
		OrderStatus orderStatus = OrderStatus.WAITING;
		double totalMoney = 0; //订单总价
		List<OrderItem> items = new ArrayList<OrderItem>();
		for(String id:orderItems.keySet()){  //获取商家
			merchant = merchantService.findByUsername(orderItems.get(id).getMerchantName());
			break;
		}
		
		for(String id:orderItems.keySet()){
			totalMoney = totalMoney + orderItems.get(id).getSubTotalMoney();
			OrderItem orderItem = new OrderItem();
			String foodId = orderItems.get(id).getId();
			Food food = foodService.findById(foodId);
			orderItem.setFood(food);
			orderItem.setTotal(orderItems.get(id).getTotal());
			orderItem.setSubTotalMoney(orderItems.get(id).getSubTotalMoney());
			items.add(orderItem);
			
		}
		orderItems = new HashMap<String, OrderItemVO>();  //清空购物车
		session.setAttribute("orderItems", orderItems);
		OrderInfo o = new OrderInfo(merchant, customer, receiverInfo, items, orderDate, orderStatus, totalMoney);
		orderInfoService.add(o);
		return "redirect: home";
		
	}
	
	
	@GetMapping("/orderInfo/customerId/{customerId}")
	public String findByCustomerId(@PathVariable("customerId") String customerId, HttpServletRequest request){
		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		orderInfos = orderInfoService.findByCustomerId(customerId);
		request.setAttribute("orderInfos", orderInfos);
		return "view/order";
		
	}
	
	@ResponseBody
	@GetMapping("/orderInfo/customerName/{customerName}")
	public List<OrderInfo> findByMerchantName(@RequestBody @PathVariable("customerName") String customerName){
		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		orderInfos = orderInfoService.findByCustomerName(customerName);
		return orderInfos;
		
	}
	
	@GetMapping("/orderInfo")
	public String countTotalMoney(Model model, HttpSession session){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		double totalMoney = 0;
		for(String foodId: orderItems.keySet()){
			totalMoney += orderItems.get(foodId).getSubTotalMoney();
		}
		Customer customer = (Customer) session.getAttribute("customer");
		List<ReceiverInfo> receiverInfos = receiverInfoService.findByCustomerId(customer.getId());
		Map<String, String> receiverInfoVOs = new HashMap<String, String>();
		for(ReceiverInfo r:receiverInfos){
			receiverInfoVOs.put(r.getId(), r.getAddress()+" "+ r.getPhone()+" "+ r.getName());
		}
		
		model.addAttribute("receiverInfoVOs", receiverInfoVOs);
		model.addAttribute("totalMoney", totalMoney);
		return "view/orderInfo";
	}
	
	@GetMapping("/orderInfo/{orderInfoId}")
	public String confirmOrderInfo(@PathVariable("orderInfoId") String orderInfoId, HttpServletRequest request){
		orderInfoService.updateOrderInfoStatus(orderInfoId);
		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		HttpSession session = request.getSession();
		Customer customer = (Customer) session.getAttribute("customer");
		orderInfos = orderInfoService.findByCustomerId(customer.getId());
		request.setAttribute("orderInfos", orderInfos);
		return "view/order";
	}
	
	
}
